package com.teamclub.app.scontrollers

import io.ebean.Ebean
import com.google.common.collect.Lists
import java.util.List

import scala.collection.JavaConversions._
import com.teamclub.domain.system._
import com.teamclub.util.libs.MD5
import io.ebean.annotation.Transactional
import io.swagger.annotations.{Api, ApiOperation}
import org.springframework.web.bind.annotation.{GetMapping, RequestMapping, RestController}

/**
  * Created by ilkkzm on 17-8-29.
  */
@RestController("com.teamclub.app.scontrollers.AppInitController")
@RequestMapping(Array("/"))
@Api
class AppInitController {

  val domainKey = "GLOBAL"

  def geneFuncTree(): List[AppFuncTree] = {
    val top = new AppFuncTree
    top.setName("系统管理")
    top.setPath("/app/system")
    top.setParentId(0)
    top.setAppDomain(domainKey)
    top.setTreeOrder(1)
    top.save()

    val app = new AppFuncTree
    app.setName("应用管理")
    app.setPath("/app/system/appmanager")
    app.setParentId(top.getId)
    app.setAppDomain(domainKey)
    app.setTreeOrder(11)
    app.save()

    val appinstance = new AppFuncTree
    appinstance.setName("应用实例管理")
    appinstance.setPath("/app/system/appinstancemanager")
    appinstance.setParentId(top.getId)
    appinstance.setAppDomain(domainKey)
    appinstance.setTreeOrder(12)
    appinstance.save()

    val usermanager = new AppFuncTree
    usermanager.setName("用户管理")
    usermanager.setPath("/app/system/usermanager")
    usermanager.setParentId(top.getId)
    usermanager.setAppDomain(domainKey)
    usermanager.setTreeOrder(13)
    usermanager.save()

    val rolemanager = new AppFuncTree
    rolemanager.setName("角色管理")
    rolemanager.setPath("/app/system/rolemanager")
    rolemanager.setParentId(top.getId)
    rolemanager.setAppDomain(domainKey)
    rolemanager.setTreeOrder(14)
    rolemanager.save()

    val lists = Lists.newArrayList(top, app, appinstance, usermanager, rolemanager)
    lists
  }

  def geneAppDomain(): AppDomain = {
    val appDomain = new AppDomain
    appDomain.setAppName("全局应用")
    appDomain.setAppKey(domainKey)
    appDomain.save()
    appDomain
  }

  def geneAppDomainInstance(): AppDomainInstance = {
    val appDomainInstance = new AppDomainInstance
    appDomainInstance.setAppDomain(domainKey)
    appDomainInstance.setName("全局应用实例")
    appDomainInstance.save()
    appDomainInstance.setOrganNo(String.valueOf(24101000 + appDomainInstance.getId))
    appDomainInstance.update()
    appDomainInstance
  }


  def geneAdminRole(funcs: List[AppFuncTree], organNo: String): AppRole = {
    val keys = funcs.map(_.getId)
    val appRole = new AppRole
    appRole.setName("管理员")
    appRole.setOrganNo(organNo)
    appRole.setRoleKey("admin")
    appRole.setFuncTreeRef(keys.mkString(","))
    appRole.save()
    appRole
  }

  /***
    *
    * @param organNo
    * @param appRole
    * @return
    */
  def geneAppSubjectUser(organNo: String, appRole: Integer): AppSubjectUser = {
    val user = new AppSubjectUser
    user.setUserName(organNo)
    user.setRealName("管理员")
    user.setUserPassword(MD5.md5Hex("admin123"))
    user.setOrganNo(organNo)
    user.setRoleId(appRole)
    user.setIsAdmin(true)
    user.save()
    user
  }

  @GetMapping(Array("/app/init"))
  @ApiOperation("初始化工程")
  def init(): String = {
    val list = Ebean.find(classOf[AppDomain]).where().eq("appKey", "GLOBAL").findList()
    if(list.size() > 0) {
      return "app has been inited"
    } else {
      val trans = Ebean.beginTransaction()
      try {
        val appdomain = geneAppDomain()
        val instance = geneAppDomainInstance()
        val funcs = geneFuncTree()
        val role = geneAdminRole(funcs, instance.getOrganNo)
        val subject= geneAppSubjectUser(instance.getOrganNo, role.getId)
        trans.commit()
      } finally {
        trans.end()
      }
      return "app init success"
    }
  }
}
